package cn.com.infcn.monitor.util;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class IFCMonitorUtils {
	// 最大
	private final static long DISK_WR_MAX_VAULE = 1024 * 1024 * 1024 * 4L;

	public static int convertInt(boolean b) {
		if (b) {
			return 1;
		} else {
			return 0;
		}
	}

	/**
	 * 获取一段时间内磁盘IO的读/写值
	 * 
	 * @param before
	 *            第一次获取的读/写值(byte)
	 * @param after
	 *            第二次获取的读/写值(byte)
	 * @return 返回读/写值差(byte)
	 */
	public static long getDiskIO(long before, long after) {
		// 如果after<before说明 after已经大于4GB大小，达到4GB后，从新清零累加
		if (after < before) {
			return (DISK_WR_MAX_VAULE - before) + after;
		} else if (after > before) {
			return after - before;
		} else {
			return 0;
		}
	}

	/**
	 * byte(字节)根据长度转成kb(千字节)和mb(兆字节)
	 * 
	 * @param bytes
	 * @return
	 */
	public static String bytes2Convert(long bytes) {
		BigDecimal filesize = new BigDecimal(bytes);
		BigDecimal gigabyte = new BigDecimal(1024 * 1024 * 1024);
		float returnValue = filesize.divide(gigabyte, 2, BigDecimal.ROUND_UP).floatValue();
		if (returnValue >= 1)
			return (returnValue + "GB");
		
		BigDecimal megabyte = new BigDecimal(1024 * 1024);
		returnValue = filesize.divide(megabyte, 2, BigDecimal.ROUND_UP).floatValue();
		if (returnValue >= 1)
			return (returnValue + "MB");
		
		BigDecimal kilobyte = new BigDecimal(1024);
		returnValue = filesize.divide(kilobyte, 2, BigDecimal.ROUND_UP).floatValue();
		return (returnValue + "KB");
	}
	
	/**
	 * 字节转KB
	 * @param bytes
	 * @return
	 */
	public static float bytes2KB(long bytes) {
		BigDecimal filesize = new BigDecimal(bytes);
		BigDecimal kilobyte = new BigDecimal(1024);
		float returnValue = filesize.divide(kilobyte, 2, BigDecimal.ROUND_UP).floatValue();
		return returnValue;
	}
	
	/**
	 * 字节转MB
	 * @param bytes
	 * @return
	 */
	public static float bytes2MB(long bytes) {
		BigDecimal filesize = new BigDecimal(bytes);
		BigDecimal megabyte = new BigDecimal(1024 * 1024);
		float returnValue = filesize.divide(megabyte, 2, BigDecimal.ROUND_UP).floatValue();
		return returnValue;
	}
	
	/**
	 * 字节转GB
	 * @param bytes
	 * @return
	 */
	public static float bytes2GB(long bytes) {
		BigDecimal filesize = new BigDecimal(bytes);
		BigDecimal megabyte = new BigDecimal(1024 * 1024 * 1024);
		float returnValue = filesize.divide(megabyte, 2, BigDecimal.ROUND_UP).floatValue();
		return returnValue;
	}
	
	
	/**
     * <p>Checks if a String is whitespace, empty ("") or null.</p>
     *
     * <pre>
     * StringUtils.isBlank(null)      = true
     * StringUtils.isBlank("")        = true
     * StringUtils.isBlank(" ")       = true
     * StringUtils.isBlank("bob")     = false
     * StringUtils.isBlank("  bob  ") = false
     * </pre>
     *
     * @param str  the String to check, may be null
     * @return <code>true</code> if the String is null, empty or whitespace
     * @since 2.0
     */
    public static boolean isBlank(String str) {
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if ((Character.isWhitespace(str.charAt(i)) == false)) {
                return false;
            }
        }
        return true;
    }
    
    /**
     * <p>Checks if a String is not empty (""), not null and not whitespace only.</p>
     *
     * <pre>
     * StringUtils.isNotBlank(null)      = false
     * StringUtils.isNotBlank("")        = false
     * StringUtils.isNotBlank(" ")       = false
     * StringUtils.isNotBlank("bob")     = true
     * StringUtils.isNotBlank("  bob  ") = true
     * </pre>
     *
     * @param str  the String to check, may be null
     * @return <code>true</code> if the String is
     *  not empty and not null and not whitespace
     * @since 2.0
     */
    public static boolean isNotBlank(String str) {
        return !IFCMonitorUtils.isBlank(str);
    }
    
    /**
     * 获取全部监控类型
     * @return
     */
    public static Set<String> getDefaultMonitorType(){
    	return getDefaultMonitorType(new HashSet<String>());
    }
    
    /**
     * 获取全部监控类型和初始化的监控类型
     * @return
     */
    public static Set<String> getDefaultMonitorType(Set<String> typeSet){
    	List<String> excludeList = IFCMonitorConstanst.excludeTypeList;
    	List<String> includeList = IFCMonitorConstanst.excludeTypeList;
    	
    	EnumSet<MonitorType> enumSet = EnumSet.allOf(MonitorType.class);
		for (MonitorType monitorType : enumSet) {
			if(monitorType.getStatus()==1){
				typeSet.add(monitorType.getName());
			}
		}
		//加上include的类型
		for (String type : includeList) {
			typeSet.add(type);
		}
		//删除已经过滤掉的
		for (String type : excludeList) {
			typeSet.remove(type);
		}
		//删除All
		typeSet.remove(MonitorType.ALL.getName());
    	return typeSet;
    }
    
    /**
     * 全部监控类型
     * @param args
     * @throws Exception
     */
    public static List<String> getAllMonitorTypeList(){
		EnumSet<MonitorType> enumSet = EnumSet.allOf(MonitorType.class);
		List<String> monitorTypeList = new ArrayList<String>();
		for (MonitorType monitorType : enumSet) {
			monitorTypeList.add(monitorType.getName());
		}
		return monitorTypeList;
	}
    
	public static void main(String[] args) throws Exception{
		System.out.println(bytes2Convert(1024*1024+1));
		float a=bytes2KB(1024*1024+1);
		float b=bytes2GB(1024*1024+1);
		System.out.println(a);
		System.out.println(b);
	}
}
